#include "CenterCameraComponent.h"

IEngineComponentRegistry centerCameraComponentRegistery("CenterCameraComponent", CenterCameraComponent::Deserialize);

CenterCameraComponent::CenterCameraComponent()
{
	pDevice = NULL;

	arc = (float)-79.931885;
	distance = (float)58.003510;
	rotation = (float)0;
}

D3DXVECTOR3 CenterCameraComponent::getPosition()
{
	D3DXVECTOR4 position;
	D3DXVECTOR3 zero(0,0,0);
	D3DXMATRIX inverseView;
	D3DXMATRIX view = getView();
	D3DXMatrixInverse(&inverseView, NULL, &view);
	D3DXVec3Transform(&position,& zero,& inverseView);

	return D3DXVECTOR3(position.x / position.w, position.y / position.w, position.z/position.w);
}

float CenterCameraComponent::getCameraArc()
{
	return arc;
}

void CenterCameraComponent::setCameraArc(float arc)
{
	this->arc = arc;

	if (this->arc > 90.0f)
		this->arc = 90.0f;
	else if (this->arc < -90.0f)
		this->arc = -90.0f;
}

float CenterCameraComponent::getCameraDistance()
{
	return distance;
}

void CenterCameraComponent::setCameraDistance(float distance)
{
	this->distance = distance;

	if (this->distance > 11900.0f)
		this->distance = 11900.0f;
	else if (this->distance < 10.0f)
		this->distance = 10.0f;
}

float CenterCameraComponent::getCameraRotation()
{
	return rotation;
}

void CenterCameraComponent::setCameraRotation(float rotation)
{
	this->rotation = rotation;
}

D3DXMATRIX CenterCameraComponent::getView()
{
	D3DXMATRIX mTranslation;
	D3DXMATRIX mRotateY;
	D3DXMATRIX mRotateX;
	D3DXMATRIX lookAt;

	D3DXMatrixIdentity(&mTranslation);
	D3DXMatrixRotationY(&mRotateY, (float)(rotation*D3DX_PI/180));
	D3DXMatrixRotationX(&mRotateX, (float)(arc*D3DX_PI/180));
	D3DXVECTOR3 Eye( 0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 Position(0.0f, 0.0f, -distance);
	D3DXVECTOR3 Up(0.0f, 1.0f, 0.0f);
	D3DXMatrixLookAtLH(&lookAt,& Position,& Eye,& Up);

	return mTranslation * mRotateY * mRotateX * lookAt;
}

float CenterCameraComponent::getNearClip()
{
	return 1.0f;
}

void CenterCameraComponent::Serialize(EnginePointer<ISerializerWriteContext>& context, char* name)
{
	context->StartItem(name, "CenterCameraComponent", "1.0");
	context->SerializeProperty("componentId", this->componentId);
	context->SerializeProperty("arc", arc);
	context->SerializeProperty("distance", distance);
	context->SerializeProperty("rotation", rotation);
	context->EndItem(name);
}

IEngineComponent* CenterCameraComponent::Deserialize(EnginePointer<ISerializerReadContext>& context)
{
	CenterCameraComponent* camera = new CenterCameraComponent();
	int index = 0;
	char* childName = context->ChildName(index);

	while (childName != NULL)
	{
		if (strcmp(childName, "componentId") == 0)
		{
			camera->componentId = context->ChildValueAsULongLong(index);
		}
		else if (strcmp(childName, "arc") == 0)
		{
			float arc = context->ChildValueAsFloat(index);
			camera->setCameraArc(arc);
		}
		else if (strcmp(childName, "distance") == 0)
		{
			float distance = context->ChildValueAsFloat(index);
			camera->setCameraDistance(distance);
		}
		else if(strcmp(childName, "rotation") == 0)
		{
			float rotation = context->ChildValueAsFloat(index);
			camera->setCameraRotation(rotation);
		}
		childName = context->ChildName(++index);
	}
	
	return camera;
}